/*
	Copyright (c) 2011 Canadensys
*/
package net.canadensys.dataportal.vascan.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.canadensys.dataportal.utils.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Query;

/**
 * A simple class to manage configuration.
 * The installation configurations are stored in properties file.
 * This class also provide function to access some configuration in the database.
 */
public class ConfigManager {
	
	private ConfigManager(){}//hide the constructor, this is a static class
	
	/**
	 * This method makes sure all config are loaded
	 * @param webappFolder The folder where this webApp is installed
	 * @param installationConfig 
	 * @param validator Validator to use against the installationConfig
	 */
	public static void loadConfiguration(String webappFolder, InstallationConfigIF installationConfig, AbstractConfigValidator validator){
		Properties props = loadFile(installationConfig.getInstallationConfigFileName());
		if(props != null){
			installationConfig.init(webappFolder, props);
			if(!validator.validateConfiguration(installationConfig)){
				Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, "Installation Configuration are NOT valid. " + 
						"Vascan can not work properly without valid configuration.");
			}
		}
	}
	
	/**
	 * Returns a Properties object base on a properties file.
	 * @param propertiesFileName
	 * @return
	 */
	public static Properties loadFile(String propertiesFileName){
		Properties properties = new Properties();
		InputStream inStream = ConfigManager.class.getClassLoader().getResourceAsStream(propertiesFileName);

		if(inStream != null){
			try{
				properties.load(inStream);
			}
			catch (IOException ioEx) {
				ioEx.printStackTrace();
				Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, "Could not load " + propertiesFileName);
			}
		}
		else{
			Logger.getLogger(ConfigManager.class.getName()).log(Level.SEVERE, "Could not load " + propertiesFileName);
		}
		return properties;
	}
	
	/**
	 * Method to load a value from the database.
	 * @param key
	 * @return value or an empty string if this key was not found
	 */
	public static String loadValueFromDB(String key){
		String value = "";
		org.hibernate.Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
		Query query = hibernateSession.createSQLQuery("SELECT `value` FROM `config` WHERE `key` = :key");
		query.setString("key", key);
		value = (String)query.uniqueResult();
		return value;
	}
	
	/**
	 * Method to save a key/value pair to the database.
	 * @param key
	 * @param value
	 * @return successfully saved or not
	 */
	public static boolean saveValueToDB(String key,String value){
		boolean success = false;
		org.hibernate.Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			if(hibernateSession.createSQLQuery("UPDATE `config` SET `value` = '" + value + "' WHERE `key` = '" + key + "' LIMIT 1").executeUpdate() > 0){
				success = true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return success;	
	}
}
